// components/search/index.js
const SEARCH_LIST = 'search_history_list'
const LIST_MAX = 50

const app = getApp()
Component({
    options: {
        addGlobalClass: true
    },
    properties: {
        placeholder: {
            type: String,
            value: '请输入要搜索的商品名称'
        },
        show: {
            type: Boolean,
            value: false
        },
        value: {
            type: String,
            value: ''
        }
    },
    data: {
        list: []
    },
    methods: {
        // 输入框双向绑定
        handleModelValue(e) {
            let value = e.detail.value
            let model = e.target.dataset.model
            this.setData({
                [model]: value
            })
        },
        handlerShowModal() {
            this.handlerHistoryList()
            this.setData({
                show: true
            })
        },
        handlerCloseModal() {
            this.setData({
                show: false
            })
        },
        // 清空输入框内容
        handlerClearValue() {
            this.setData({
                value: ''
            })
            this.triggerEvent('search', this.data.value)
            this.triggerEvent('clear')
        },
        // 开始搜索
        handlerSearch() {
            const val = this.data.value
            this.setData({
                value: val
            })
            this.createSearchValue(val)
            this.setData({
                show: false,
            })
            console.log('search-->', this.data.value)
            this.triggerEvent('search', this.data.value)
            app.mdFn({
                eventId: '1005',
                eventDesc: '搜索',
            })
        },
        handlerItem(e) {
            const value = e.currentTarget.dataset.name
            this.setData({
                value
            })
            this.handlerSearch()
        },
        // 缓存搜索名称
        createSearchValue(val) {
            if (!val.trim()) {
                return
            }
            let list = this.getSearchList()

            const len = list.length
            let obj = {
                name: val.trim(),
                time: Date.now()
            }
            if (len === 0) {
                list.push(obj)
                wx.setStorageSync(SEARCH_LIST, list)
            } else {
                if (len >= LIST_MAX) {
                    list.splice(0, 1)
                }
                let index
                let isInc = list.filter((item, i) => {
                    if (item.name === val) {
                        index = i
                        return true
                    } else {
                        return false
                    }
                })
                if (isInc.length > 0) {
                    list.splice(index, 1)
                }
                list.push(obj)
                console.log(list)
                wx.setStorageSync(SEARCH_LIST, list)
            }
            this.handlerHistoryList()
        },
        // 获取搜索列表
        getSearchList() {
            let list = []
            try {
                list = wx.getStorageSync(SEARCH_LIST) || []
            } catch (e) {
                list = []
                console.error(e)
            }
            return list
        },
        // 清空搜索列表
        clearSearchList() {
            wx.clearStorageSync(SEARCH_LIST)
            this.handlerHistoryList()
        },
        handlerHistoryList() {
            this.setData({
                list: this.getSearchList().reverse()
            })
        }
    }
})
